/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.orm.datasource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import com.yunji.framework_template.common.util.StringUtil;

/**
 * ClassName:DynamicDataSource <br/>
 * Date: 2017年8月16日 上午9:07:06 <br/>
 * 
 * @author fenglibin
 * @version
 * @see
 */

public class DynamicDataSource extends AbstractRoutingDataSource {

    private static final Logger logger = Logger.getLogger(DynamicDataSource.class);
    /**
     * 该参数用在多国家环境下时，则当前应用的数据源只会时当前指定国家的数据源，
     * 不会根据用户来源的国家，将用户的数据源，切换到其它国家。
     */
    @Value("#{settings['onlyCountry']}")
    private String              onlyCountry;

    /**
     * 获取与数据源相关的key 此key是Map<String,DataSource> resolvedDataSources 中与数据源绑定的key值 在通过determineTargetDataSource获取目标数据源时使用
     */
    @Override
    protected Object determineCurrentLookupKey() {
        if(!StringUtil.isEmpty(onlyCountry)) {
            //logger.info("current lookup key is ONLY country:" + onlyCountry);
            return onlyCountry;
        }
        Object obj = HandleDataSource.getDataSource();
        //logger.info("current lookup key is:" + obj);
        return obj;
    }

}
